﻿using System;
using SimpleServiceBus.EndpointMonitor.Web.Helpers.Flot.FlotOptions;

namespace SimpleServiceBus.EndpointMonitor.Web.Helpers.Flot
{
    public static class FlotChartDefaults
    {
        public static Action<FlotChartViewData> Global = chart =>
                                                             {
                                                                 chart.GlobalOptions.ShadowSize = 0;
                                                                 chart.GlobalOptions.Grid.TickColor = "rgba(0,0,0,0)";
                                                                 chart.GlobalOptions.Grid.BorderWidth = 1;
                                                                 chart.GlobalOptions.Grid.Hoverable = true;
                                                                 chart.GlobalOptions.Legend.Show = true;
                                                                 chart.GlobalOptions.Legend.Position = FlotLegendPosition.Nw;
                                                                 chart.GlobalOptions.Colors = new[] { "#5c87b2", "#CCCCCC" };
                                                                 chart.GlobalOptions.Xaxis.Mode = FlotAxisMode.Time;
        
                                                             };

        public static Action<FlotChartViewData> LineGraph = chart =>
                                                                {
                                                                    chart.GlobalOptions.Lines.Show = true;
                                                                    chart.GlobalOptions.Lines.LineWidth = 3;

                                                                    chart.GlobalOptions.Colors = new[] { "#5c87b2", "#CCCCCC" };
                                                                };

        public static Action<FlotChartViewData> BarGraph = chart =>
                                                               {
                                                                   chart.GlobalOptions.Bars.Show = true;
                                                                   chart.GlobalOptions.Bars.Fill = 1;
                                                               };

        public static Action<FlotChartViewData> TimeSeriesByMonth = chart =>
                                                                        {
                                                                            chart.GlobalOptions.Xaxis.Mode = FlotAxisMode.Time;
                                                                            chart.GlobalOptions.Xaxis.TickSize = "[2, \"month\"]";
                                                                        };

        public static Action<FlotChartViewData> TimeSeriesByYear = chart =>
                                                                       {
                                                                           chart.GlobalOptions.Xaxis.Mode = FlotAxisMode.Time;
                                                                           chart.GlobalOptions.Xaxis.TickSize = "[1, \"year\"]";
                                                                       };

        public static Action<FlotChartViewData> TimeSeriesByDay = chart =>
                                                                      {
                                                                          chart.GlobalOptions.Xaxis.Mode = FlotAxisMode.Time;
                                                                          chart.GlobalOptions.Xaxis.TickSize = "[5, \"day\"]";
                                                                          chart.GlobalOptions.Xaxis.Timeformat = "%m/%d";
                                                                      };

        public static Action<FlotChartViewData> TimeSeriesByHour = chart =>
                                                                       {
                                                                           chart.GlobalOptions.Xaxis.Mode = FlotAxisMode.Time;
                                                                           chart.GlobalOptions.Xaxis.TickSize = "[2, \"hour\"]";
                                                                           chart.GlobalOptions.Xaxis.Timeformat = "%H";
                                                                       };

        public static Action<FlotChartViewData> Sum12MonthsVsPriorYear = chart =>
                                                                         ConfigureSeriesVsSeriesBars(chart, "12 Month Sum", "12 Month Sum Pr Yr");

        public static Action<FlotChartViewData> DiscreteXAxis = chart =>
                                                                    {
                                                                        const int X_AXIS_INDEX = 0;
                                                                        SetDiscreteDefaults(chart, X_AXIS_INDEX, chart.GlobalOptions.Xaxis);
                                                                        chart.DiscreteAxis = Axes.X;
                                                                        //Ensure a zero starting point for the opposite axis
                                                                        chart.GlobalOptions.Yaxis.Min = 0;
                                                                    };

        public static Action<FlotChartViewData> DiscreteYAxis = chart =>
                                                                    {
                                                                        const int Y_AXIS_INDEX = 1;
                                                                        SetDiscreteDefaults(chart, Y_AXIS_INDEX, chart.GlobalOptions.Yaxis);
                                                                        chart.DiscreteAxis = Axes.Y;
                                                                        //Ensure a zero starting point for the opposite axis
                                                                        chart.GlobalOptions.Xaxis.Min = 0;
                                                                    };

        private static void ConfigureSeriesVsSeriesBars(FlotChartViewData chart,string series1,string series2)
        {
            chart.AddNewSeriesOptions(series1, options => options.Bars.BarWidth = .7);
            chart.AddNewSeriesOptions(series2, options => options.Bars.BarWidth = .4);
        }

        private static void SetDiscreteDefaults(FlotChartViewData chart, int axisIndex, FlotAxisOptions axisOptions)
        {
            chart.GlobalOptions.Xaxis.Mode = FlotAxisMode.None;
            chart.GlobalOptions.Yaxis.Mode = FlotAxisMode.None;
            axisOptions.TickSize = 1.ToString();
            axisOptions.TickFormatter = "function (val, axis) { " +
                                        string.Format(DISCRETE_SERIES_TICK_FORMATTER_TEMPLATE, axisIndex, chart.MaxAxisLabelLength) + "}";
            chart.GlobalOptions.Bars.Show = true;
            chart.GlobalOptions.Bars.Align = FlotBarAlignment.Center;
            chart.GlobalOptions.Bars.Horizontal = true;
            chart.ReturnCaptions = true;
            chart.ReturnIdentifiers = true;
        }

        private const string DISCRETE_SERIES_TICK_FORMATTER_TEMPLATE = "return $.getDiscreteAxisLabel(data,val,{0},{1});";
    }
}